package com.xjtu.duansheli.controller;


import com.xjtu.duansheli.entity.ProductCommentsInfo;
import com.xjtu.duansheli.entity.ProductInfo;
import com.xjtu.duansheli.entity.UserInfo;
import com.xjtu.duansheli.json.AjaxResponse;
import com.xjtu.duansheli.service.ProductCommentsInfoService;
import com.xjtu.duansheli.service.ProductImagesInfoService;
import com.xjtu.duansheli.service.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Slf4j
@RestController
@RequestMapping("/project/product-comments-info")
@Api(value = "商品评论信息管理模块", description = "这是商品评论信息增删改查的模块")
public class ProductCommentsInfoController {

    @Autowired
    private ProductCommentsInfoService productCommentsInfoService;


    @ApiOperation(value = "获取全部商品评论", notes = "获取所有商品评论的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {
        return AjaxResponse.success(productCommentsInfoService.getAllProductCom(current, size));
    }


    @ApiOperation(value = "根据id查找商品评论", notes = "获取根据id查找到的商品评论")
    @ApiImplicitParam(name = "id", value = "评论id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getProductCommById", method = RequestMethod.GET)
    public AjaxResponse getProductCommById(@RequestParam(name = "id") Long id) {
        return AjaxResponse.success(productCommentsInfoService.getProductComByid(id));
    }

    @ApiOperation(value = "根据商品id查找对应的评论", notes = "查找某个商品的全部评论")
    @ApiImplicitParam(name = "pid", value = "商品id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getProductCommBypId", method = RequestMethod.GET)
    public AjaxResponse getProductCommBypId(@RequestParam(name = "pid") Long pid) {
        return AjaxResponse.success(productCommentsInfoService.getProductComBypid(pid));
    }

    @ApiOperation(value = "根据用户id查找他发表的全部评论", notes = "查找某个用户发表过的的全部评论")
    @ApiImplicitParam(name = "aid", value = "用户id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getProductCommByaId", method = RequestMethod.GET)
    public AjaxResponse getProductCommByaId(@RequestParam(name = "aid") Long aid) {
        return AjaxResponse.success(productCommentsInfoService.getProductComByaid(aid));
    }

    @ApiOperation(value = "根据评论id查找他的子评论", notes = "查找某条评论的全部子评论")
    @ApiImplicitParam(name = "paid", value = "评论id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getProductCommBypaId", method = RequestMethod.GET)
    public AjaxResponse getProductCommBypaId(@RequestParam(name = "paid") Long paid) {
        return AjaxResponse.success(productCommentsInfoService.getProductComByparentId(paid));
    }

    @ApiOperation(value = "添加商品评论信息", notes = "获取商品评论信息并且添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "评论内容", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "productId", value = "所属商品id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "parentId", value = "所属父评论id", required = false, defaultValue = "-1", paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "/addProductComm", method = RequestMethod.POST)
    public AjaxResponse addProductComm(@RequestParam(value = "content") String content,
                                       @RequestParam(value = "productId") Long productId,
                                       @RequestParam(value = "parentId") Long parentId,
                                       HttpServletRequest request) {
        UserInfo user = (UserInfo) request.getSession().getAttribute("loginUser");
        ProductCommentsInfo productCommentsInfo = new ProductCommentsInfo()
                .setContent(content)
                .setProductId(productId)
                .setAuthorId(user.getUid())
                .setParentId(parentId);
        return AjaxResponse.success("数据库受影响行数->" + productCommentsInfoService.addProductCom(productCommentsInfo));
    }

    @ApiOperation(value = "修改商品评论信息", notes = "录入新的商品评论信息并且修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "评论id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "content", value = "评论内容", required = false, paramType = "form", dataType = "String")
    })
    @RequestMapping(value = "/updateProductComm", method = RequestMethod.PUT)
    public AjaxResponse updateProductComm(@RequestParam(value = "content") String content,
                                          @RequestParam(value = "cid") Long cid) {
        ProductCommentsInfo productCommentsInfo = new ProductCommentsInfo()
                .setCid(cid)
                .setContent(content);
        return AjaxResponse.success("数据库受影响行数->" + productCommentsInfoService.updateProductCom(productCommentsInfo));
    }

    @ApiOperation(value = "删除商品评论信息", notes = "从数据库中删除某个商品评论")
    @ApiImplicitParam(name = "cid", value = "删除商品评论id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteProductComm", method = RequestMethod.DELETE)
    public AjaxResponse deleteProductComm(@RequestParam(value = "cid") Long cid) {
        return AjaxResponse.success("数据库受影响行数->" + productCommentsInfoService.deleteProductCom(cid));
    }

    @ApiOperation(value = "商品评论总数量", notes = "获取数据库中商品评论总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(productCommentsInfoService.getTotal());
    }
}

